#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
/* widgets:
Layer_Config_DemoMode
Background_ConfigDemoMode
Container_Demo_CloseOrNot
PopupButton_CloseOrNot_Cancel
PopupButton_CloseOrNot_Close
Container_Demo_OpenOrNot
PopupButton_OpenOrNot_Open
PopupButton_OpenOrNot_Cancel
*/
static ITUBackground* Background_ConfigDemoMode;
static ITUContainer* DemoModeContainer_Demo_OpenOrNot;
static ITUContainer* DemoModeContainer_Demo_CloseOrNot;
static ITUPopupButton* DemoModePopupButton_CloseOrNot_Cancel;
static ITUPopupButton* DemoModePopupButton_CloseOrNot_Close;
static ITUPopupButton* DemoModePopupButton_OpenOrNot_Open;
static ITUPopupButton* DemoModePopupButton_OpenOrNot_Cancel;
bool Config_DemoModeOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_ConfigDemoMode)
	{
		Background_ConfigDemoMode = ituSceneFindWidget(&theScene, "Background_ConfigDemoMode");
		assert(Background_ConfigDemoMode);
		DemoModeContainer_Demo_OpenOrNot = ituSceneFindWidget(&theScene, "DemoModeContainer_Demo_OpenOrNot");
		assert(DemoModeContainer_Demo_OpenOrNot);
		DemoModeContainer_Demo_CloseOrNot = ituSceneFindWidget(&theScene, "DemoModeContainer_Demo_CloseOrNot");
		assert(DemoModeContainer_Demo_CloseOrNot);
		DemoModePopupButton_CloseOrNot_Cancel = ituSceneFindWidget(&theScene, "DemoModePopupButton_CloseOrNot_Cancel");
		assert(DemoModePopupButton_CloseOrNot_Cancel);
		DemoModePopupButton_CloseOrNot_Close = ituSceneFindWidget(&theScene, "DemoModePopupButton_CloseOrNot_Close");
		assert(DemoModePopupButton_CloseOrNot_Close);
		DemoModePopupButton_OpenOrNot_Open = ituSceneFindWidget(&theScene, "DemoModePopupButton_OpenOrNot_Open");
		assert(DemoModePopupButton_OpenOrNot_Open);
		DemoModePopupButton_OpenOrNot_Cancel = ituSceneFindWidget(&theScene, "DemoModePopupButton_OpenOrNot_Cancel");
		assert(DemoModePopupButton_OpenOrNot_Cancel);
	}
	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{

	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		//PopupButton_CloseOrNot_Cancel->btn.text.string = "關閉";
		//PopupButton_CloseOrNot_Close->btn.text.string = "開啓";
		//PopupButton_OpenOrNot_Open->btn.text.string = "開啓";
		//PopupButton_OpenOrNot_Cancel->btn.text.string = "關閉";
		DemoModePopupButton_CloseOrNot_Cancel->btn.text.fontIndex = 8;
		DemoModePopupButton_CloseOrNot_Close->btn.text.fontIndex = 8;
		DemoModePopupButton_OpenOrNot_Open->btn.text.fontIndex = 8;
		DemoModePopupButton_OpenOrNot_Cancel->btn.text.fontIndex = 8;
	}
	//根据参数显示界面
	ituWidgetSetVisible(DemoModeContainer_Demo_OpenOrNot, false);
	ituWidgetSetVisible(DemoModeContainer_Demo_CloseOrNot, false);
	if (Sys.IsDemoMode)
		ituWidgetSetVisible(DemoModeContainer_Demo_OpenOrNot, true);
	else
		ituWidgetSetVisible(DemoModeContainer_Demo_CloseOrNot, true);
    return true;
}

bool Config_DemoModeOnLeave(ITUWidget* widget, char* param)
{
	Clear_User_Action();
	return true;
}

bool Config_DemoModeOnTimer(ITUWidget* widget, char* param)
{
    return true;
}

bool SetDemoMode(ITUWidget* widget, char* param)
{
	uint8_t Input_Variable;
	bool Temp_Data = Sys.IsDemoMode;
	Input_Variable = atoi(param);
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoConfigLayer();//返回设置界面
		Clear_User_Action();
	}
	if (Input_Variable)
	{
		Sys.IsDemoMode = true;
		//设置演示参数
		Set_Debug_Dev_Config();
	}
	else
	{
		Sys.IsDemoMode = false;
		//读取设备参数
		Read_Device_Config();
	}

	//判断是否需要保存
	if (Temp_Data != Sys.IsDemoMode)
		MessageFlag[MSG_SAVE_SYS] = ON;
    return true;
}

